package com.anjoyo.xgh.musicshare.activity;

import java.io.File;

import android.app.Activity;
import android.app.AlertDialog;
import android.content.DialogInterface;
import android.content.Intent;
import android.graphics.Bitmap;
import android.net.Uri;
import android.os.Bundle;
import android.os.Environment;
import android.os.Handler;
import android.os.Message;
import android.provider.MediaStore;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.RadioGroup;
import android.widget.Toast;

import com.anjoyo.xgh.musicshare.R;
import com.anjoyo.xgh.musicshare.api.UserApi;
import com.anjoyo.xgh.musicshare.util.BitmapUtil;
import com.anjoyo.xgh.musicshare.util.Constant;
import com.anjoyo.xgh.musicshare.util.StringUtil;


public class RegistAct extends Activity {

	private static final int PHOTO_REQUEST_TAKEPHOTO = 1;// 拍照
	private static final int PHOTO_REQUEST_GALLERY = 2;// 从相册中
	private static final int PHOTO_REQUEST_CUT = 3;// 结果

	File tempFile = new File(Environment.getExternalStorageDirectory(),
			StringUtil.getPhotoFileName());
	String UPLOADPATH = Constant.CACHE + "head.png";
    RadioGroup radiogroup;
	ImageView ivheader;
	EditText etname, etpwd, etpwd2, etnickname, etdesrption;
	Button btregist;
	ImageView back;

	Handler handler=new Handler(){
		@Override
		public void handleMessage(Message msg) {

			if(msg.what==Constant.MSG_REGIST_OK){
				if(msg.arg1==200){
					Toast("注册成功");
				}else{
					Toast("注册失败");
				}
				System.out.println(msg.obj.toString());
			}
			super.handleMessage(msg);
		}
	};
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.regist_layout);
		initview();
		initListener();
	}

	private void initListener() {
		ivheader.setOnClickListener(new OnClickListener() {
			@Override
			public void onClick(View arg0) {
				ChosePhotosDialog();
			}
		});
		back.setOnClickListener(new OnClickListener() {
			@Override
			public void onClick(View arg0) {
				finish();
				overridePendingTransition(R.anim.roll_up, R.anim.roll);
			}
		});
		btregist.setOnClickListener(new OnClickListener() {
			@Override
			public void onClick(View v) {
				String name = etname.getText().toString();
				String password = etpwd.getText().toString();
				String password2 = etpwd2.getText().toString();
				String nickname = etnickname.getText().toString();
				String descrption = etdesrption.getText().toString();
				if (name.trim().length() < 3 || password.trim().length() < 3) {
					Toast("用户名和密码长度不对");
					return;
				}
				if (!password.equals(password2)) {
					Toast("密码前后输入不一致");
					return;
				}
				String sex="";
				if(radiogroup.getCheckedRadioButtonId()==R.id.reg_rb_m){
					sex="m";
				}else{
					sex="w";
				}
				File file=new File(UPLOADPATH);
				if(file.exists()){
					UserApi api=new UserApi(handler);
				    api.regist(name, password, nickname, descrption, sex, file);
				}else{
					Toast("请选择头像");
				}
			}
		});
	}

	private void initview() {
		ivheader = (ImageView) findViewById(R.id.reg_iv_chosehead);
		etname = (EditText) findViewById(R.id.reg_ed_username);
		etpwd = (EditText) findViewById(R.id.reg_ed_password);
		etpwd2 = (EditText) findViewById(R.id.reg_ed_password2);
		etnickname = (EditText) findViewById(R.id.reg_ed_nickname);
		etdesrption = (EditText) findViewById(R.id.reg_ed_desrption);
		btregist = (Button) findViewById(R.id.reg_bt_regist);
		back = (ImageView) findViewById(R.id.back);
		radiogroup=(RadioGroup) findViewById(R.id.reg_chose_sex);
	}

	public void Toast(String msg) {
		Toast.makeText(RegistAct.this, msg, Toast.LENGTH_SHORT).show();
	}

	private void ChosePhotosDialog() {
		new AlertDialog.Builder(RegistAct.this)
				.setTitle("选择头像")
				.setPositiveButton("拍照", new DialogInterface.OnClickListener() {
					@Override
					public void onClick(DialogInterface dialog, int which) {
						dialog.dismiss();
						Intent intent = new Intent(
								MediaStore.ACTION_IMAGE_CAPTURE);
						// 指定调用相机拍照后照片的储存路径
						intent.putExtra(MediaStore.EXTRA_OUTPUT,
								Uri.fromFile(tempFile));
						startActivityForResult(intent, PHOTO_REQUEST_TAKEPHOTO);
					}
				})
				.setNegativeButton("相册", new DialogInterface.OnClickListener() {

					@Override
					public void onClick(DialogInterface dialog, int which) {
						dialog.dismiss();
						Intent intent = new Intent(Intent.ACTION_PICK, null);
						intent.setDataAndType(
								MediaStore.Images.Media.EXTERNAL_CONTENT_URI,
								"image/*");
						startActivityForResult(intent, PHOTO_REQUEST_GALLERY);
					}
				}).create().show();
	}

	@Override
	protected void onActivityResult(int requestCode, int resultCode, Intent data) {
		switch (requestCode) {
		case PHOTO_REQUEST_TAKEPHOTO:
			startPhotoZoom(Uri.fromFile(tempFile), 150);
			break;
		case PHOTO_REQUEST_GALLERY:
			if (data != null)
				startPhotoZoom(data.getData(), 150);
			break;
		case PHOTO_REQUEST_CUT:
			if (data != null)
				setPicToView(data);
			break;
		}
		super.onActivityResult(requestCode, resultCode, data);
	}

	// 将进行剪裁后的图片显示到UI界面上
	private void setPicToView(Intent picdata) {
		Bundle bundle = picdata.getExtras();
		if (bundle != null) {
			Bitmap photo = bundle.getParcelable("data");
			BitmapUtil.saveImg(UPLOADPATH, photo);
			ivheader.setImageBitmap(photo);
		}
	}

	private void startPhotoZoom(Uri uri, int size) {
		Intent intent = new Intent("com.android.camera.action.CROP");
		intent.setDataAndType(uri, "image/*");
		// crop为true是设置在开启的intent中设置显示的view可以剪裁
		intent.putExtra("crop", "true");
		// aspectX aspectY 是宽高的比例
		intent.putExtra("aspectX", 1);
		intent.putExtra("aspectY", 1);
		// outputX,outputY 是剪裁图片的宽高
		intent.putExtra("outputX", size);
		intent.putExtra("outputY", size);
		intent.putExtra("return-data", true);
		startActivityForResult(intent, PHOTO_REQUEST_CUT);
	}

}
